import {
  getGroupDetailTable
} from '../../api/sms'
import {
  GROUP_DETAIL_LIST,
  GROUP_DETAIL_DATA_COUNT,
  GROUP_DETAIL_HISTORY_DATA,
  GROUP_DETAIL_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    groupDetailList: [],
    groupDetailDataCount: null,
    groupDetailPageSize: 10,
    groupDetailHistoryData: [],
    row: {
      ctime: '',
      mname: '',
      uname: '',
      tel: '',
      state0: '',
      state: '',
      cprice: ''
    }
  },
  getters: {
    groupDetailList: state => state.groupDetailList
  },
  mutations: {
    [GROUP_DETAIL_LIST] (state, data) {
      state.groupDetailList = data
    },
    [GROUP_DETAIL_DATA_COUNT] (state, data) {
      state.groupDetailDataCount = data
    },
    [GROUP_DETAIL_HISTORY_DATA] (state, data) {
      state.groupDetailHistoryData = data
    },
    [GROUP_DETAIL_PAGE_SIZE] (state, data) {
      state.groupDetailPageSize = data
    }
  },
  actions: {
    async getGroupDetailList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getGroupDetailTable(data)
      resp.data.data.rows.map((item, index) => {
        newData = [...newData, {
          id: item.id,
          ctime: item.ctime,
          mname: item.mname,
          uname: item.uname,
          tel: item.tel,
          state0: item.state0,
          state: item.state,
          cprice: item.cprice
        }]
      })
      commit(GROUP_DETAIL_LIST, newData)
      commit(GROUP_DETAIL_DATA_COUNT, newData.length)
      if (newData.length < rootState.groupDetail.groupDetailPageSize) {
        commit(GROUP_DETAIL_HISTORY_DATA, newData)
      } else {
        commit(GROUP_DETAIL_HISTORY_DATA, newData.slice(0, rootState.groupDetail.groupDetailPageSize))
      }
    },
    async changeGroupDetailPage ({ commit, rootState }, index) {
      let data = rootState.groupDetail.groupDetailList
      let _start = (index - 1) * rootState.groupDetail.groupDetailPageSize
      let _end = index * rootState.groupDetail.groupDetailPageSize
      commit(GROUP_DETAIL_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeGroupDetailPageSize ({ commit, rootState }, value) {
      commit(GROUP_DETAIL_PAGE_SIZE, value)
    }
  }
}
